<?php
/**
 * Created by PhpStorm.
 * User: 刘抱
 * Date: 2018/9/21
 * Time: 10:16
 */

namespace Home\Controller;


use Home\Services\BaseService;

class OldToNewController extends CommunalController
{
    //老项目表导入
    public function project()
    {
        $db_crm_project = M("db_crm_project");
        $res_old_project = M("db_project")->where("center_id=130003")->select();
        foreach ($res_old_project as $k => $v) {
            $new = array();
            $new['project_id'] = $v['id'];
            $new['information_id'] = $v['info_id'];
            $new['project_num'] = $v['project_num'];
            $new['name'] = $v['project_name'];
            $new['short_name'] = $v['key_word'];
            $new['leader'] = $v['director_uid'];
            $new['other_linkman'] = $v['cid'];
            $new['nature'] = 2;
            $new['type'] = 1;
            $new['add_uid'] = $v['add_uid'];
            $new['add_date'] = $v['add_date'];
            $db_crm_project->add($new);
        }
        echo '执行完毕';
        die;
    }

    //更新项目简介
    public function project_bg()
    {
        $db_crm_project = M("db_crm_project");
        $db_project_bg = M("db_project_bg");
        $res_crm_project = $db_crm_project->select();
        $res_project_bg = $db_project_bg->select();
        foreach ($res_crm_project as $k => $v) {
            foreach ($res_project_bg as $m => $n) {
                if ($v['project_id'] == $n['pid']) {
                    $project_id = $v['project_id'];
                    $data['intro'] = $n['project_introduce'];
                    $data['work_demands'] = $n['cooperative_appeals'];
                    $data['backdrop'] = $n['party_background'];
                    $db_crm_project->where("project_id=$project_id")->save($data);
                }
            }
        }
        echo '执行完毕';
        die;
    }

    public function project_funds()
    {
        $db_project_funds = M("db_project_funds as funds");
        $db_contract = M("db_contract");
        /* $res_project_funds=$db_project_funds->select();
         $funds_project_ids = array_column($res_project_funds,'pid');
         $where_bg['pid'] = array('in',$funds_project_ids);
         $res_project_bg = $db_project_bg->where($where_bg)->select();*/

        $res_project_funds = $db_project_funds->field('funds.*,bg.bus_id')->join('left join db_project_bg as bg on funds.pid=bg.pid')->select();
        try {
            foreach ($res_project_funds as $k => $v) {
                $new = array();
                $new['contract_id'] = $v['id'];
                $new['center_id'] = '130003';
                $new['project_id'] = $v['bus_id'];
                $new['number'] = 'HTO00' . $v['id'];
                $new['file_name'] = $v['file_name'];
                $new['file_url'] = $v['file_url'];
                $new['contract_money'] = $v['contract_money'];
                $new['sign_contract_date'] = $new['sign_start_date'] = $v['contract_date'];
                $new['sign_end_date'] = $v['actual_end_date'];
                $new['sign_user_id'] = $new['add_uid'] = $v['add_uid'];
                $new['status'] = 1;
                $new['back_count'] = $v['stages'];
                $new['add_date'] = $v['add_date'];
                $db_contract->add($new);
            }
            echo '执行完毕';
            die;
        } catch (\Exception $e) {
            echo '执行失败';
            die;
        }


    }

    //收款计划
    public function stage()
    {
        $db_project_funds_stage = M("db_project_funds_stage");
        $db_contract_back = M("db_contract_back");
        $db_project_funds = M("db_project_funds as funds");
        $res_project_funds = $db_project_funds->field('funds.*,bg.bus_id')->join('left join db_project_bg as bg on funds.pid=bg.pid')->select();
        $res_funds_stage = $db_project_funds_stage->select();
        foreach ($res_funds_stage as $k => $v) {
            foreach ($res_project_funds as $m => $n) {
                if ($v['mid'] == $n['id']) {
                    $res_funds_stage[$k]['project_id'] = !empty($n['bus_id']) ? $n['bus_id'] : '';
                    $res_funds_stage[$k]['add_uid'] = $n['add_uid'];
                    $res_funds_stage[$k]['add_date'] = $n['add_date'];
                }
            }
            $new = array();
            $new['back_id'] = $v['id'];
            $new['contract_id'] = $v['mid'];
            $new['project_id'] = $res_funds_stage[$k]['project_id'];
            $new['stage'] = $v['stage'];
            $new['stage_money'] = $v['stage_money'];
            $new['stage_date'] = $v['stage_date'];
            $new['back_status'] = 3;
            $new['type'] = 1;
            $new['add_uid'] = $res_funds_stage[$k]['add_uid'];
            $new['add_date'] = $res_funds_stage[$k]['add_date'];
            $db_contract_back->add($new);
        }
        echo '执行完毕';
        die;
    }

    //发票
    public function ticket()
    {
        $db_project_funds_ticket = M("db_project_funds_ticket");
        $db_contract_invoice = M("db_contract_invoice");
        $db_contract_back = M("db_contract_back");
        $res_funds_ticket = $db_project_funds_ticket->select();
        $res_contract_back = $db_contract_back->select();
        foreach ($res_funds_ticket as $k => $v) {
            foreach ($res_contract_back as $m => $n) {
                if ($v['tid'] == $n['back_id']) {
                    $res_funds_ticket[$k]['contract_id'] = $n['contract_id'];
                    $res_funds_ticket[$k]['project_id'] = $n['project_id'];
                    $res_funds_ticket[$k]['back_id'] = $n['back_id'];
                }
                $new = array();
                $new['invoice_id'] = $v['id'];
                $new['number'] = 'FPO00' . $v['id'];
                $new['no'] = $v['invoice'];//发票号码
                $new['proposer'] = $new['add_uid'] = $v['add_uid'];
                $new['contract_id'] = $res_funds_ticket[$k]['contract_id'];
                $new['back_id'] = $res_funds_ticket[$k]['back_id'];
                $new['project_id'] = $res_funds_ticket[$k]['project_id'];
                $new['make_date'] = $v['date'];
                $new['make_money'] = $v['money'];
                $new['make_money'] = $v['money'];
                $new['add_date'] = $v['add_date'];
                $db_contract_invoice->add($new);
            }
        }
        echo '执行完毕';
        die;
    }

    public function money()
    {
        $db_contract_receipt = M("db_contract_receipt");
        $db_project_funds_money = M("db_project_funds_money");
        $db_contract_invoice = M("db_contract_invoice");
        $res_funds_money = $db_project_funds_money->select();
        $res_contract_invoice = $db_contract_invoice->select();
        foreach ($res_funds_money as $k => $v) {
            foreach ($res_contract_invoice as $m => $n) {
                if ($v['sid'] == $n['invoice_id']) {
                    $res_funds_money[$k]['contract_id'] = $n['contract_id'];
                    $res_funds_money[$k]['back_id'] = $n['back_id'];
                    $res_funds_money[$k]['project_id'] = $n['project_id'];
                }
                $new = array();
                $new['receipt_id'] = $v['id'];
                $new['contract_id'] = $res_funds_money[$k]['contract_id'];
                $new['project_id'] = $res_funds_money[$k]['project_id'];
                $new['back_id'] = $res_funds_money[$k]['back_id'];
                $new['invoice_id'] = $v['sid'];
                $new['money'] = $v['money'];
                $new['date'] = $v['date'];
                $new['add_uid'] = $v['add_uid'];
                $new['add_date'] = $v['add_date'];
                $db_contract_receipt->add($new);
            }
        }
        echo '执行完毕';
        die;
    }

    //更新项目状态
    public function update_status()
    {
        $db_project_bg = M("db_project_bg");
        $db_crm_project = M("db_crm_project");
        $bus_ids = $db_project_bg->where("bus_id is not null")->field('bus_id')->select();
        $bus_ids = array_column(array_unique($bus_ids, SORT_REGULAR), 'bus_id');
        $pids = M("db_project")->where("status=-2 and center_id=130003")->field('id')->select();
        $pids = array_column($pids, 'id');
        $where_bus['project_id'] = array('in', $bus_ids);
        $data_bus['stage'] = 5;//签约的有合同的项目
        $where_pid['project_id'] = array('in', $pids);
        $data_pid['stage'] = 6;//流失的
        $where_other['stage'] = array('EXP', 'IS NULL');
        $data_other['stage'] = 3;//其他的当做商务谈判
        try {
            $db_crm_project->where($where_bus)->save($data_bus);
            $db_crm_project->where($where_pid)->save($data_pid);
            $db_crm_project->where($where_other)->save($data_other);
            echo '执行成功';
        } catch (\Exception $e) {
            echo '执行失败';
        }
    }

    //更新老运营项目到新表
    public function projects()
    {
        $db_projects = M("db_projects");
        $db_project_bg = M("db_project_bg");
        $res_project = M("db_project")->where("center_id=130005")->select();
        $project_ids = array_column($res_project, 'id');
        $where_bg['pid'] = array('in', $project_ids);
        $res_bg = $db_project_bg->where($where_bg)->select();
        foreach ($res_project as $k => $v) {
            foreach ($res_bg as $bg_k => $bg_v) {
                if ($v['id'] == $bg_v['pid']) {
                    $new = array();
                    $new['pid'] = $v['id'];
                    $new['project_id'] = $bg_v['bus_id'];
                    $new['project_num'] = $v['project_num'];
                    $new['project_name'] = $v['project_name'];
                    $new['short_name'] = $v['key_word'];
                    $new['department_id'] = $v['dept_id'];
                    $new['leader'] = $v['director_uid'];
                    $new['group_leader'] = $v['manager_uid'];
                    $new['main_linkman'] = $v['cid'];
                    if ($v['status'] == '-1') {
                        $new['stage'] = 5;
                    } elseif ($v['status'] == '-2') {
                        $new['stage'] = 3;
                    } else {
                        $new['stage'] = $v['status'];
                    }
                    $new['start_date'] = $v['start_date'];
                    $new['add_uid'] = $v['add_uid'];
                    $new['add_date'] = $v['add_date'];
                    $db_projects->add($new);
                }

            }

        }
        echo '执行完毕';
        die;
    }

    //周报更新到新表
    public function weekly()
    {
        $db_project_operate = M("db_project_operate");
        $db_projects_weekly = M("db_projects_weekly");
        $db_project_operate_detail = M("db_project_operate_detail");
        $db_project_operate_answer = M("db_project_operate_answer");
        $res_old_weekly = $db_project_operate->select();
        $res_old_weekly_detail = $db_project_operate_detail->select();
        $res_old_weekly_detail = BaseService::array_new($res_old_weekly_detail, 'mid');
        $res_detail = array();
        foreach ($res_old_weekly_detail as $k => $detail) {
            $res_detail[$k]['content'] = implode(PHP_EOL, array_column($detail, 'content'));
        }
        $res_weekly_reply = $db_project_operate_answer->select();
        foreach ($res_detail as $k => $v) {
            foreach ($res_old_weekly as $value) {
                if ($value['id'] == $k) {
                    $new = array();
                    $new['pid'] = $value['pid'];
                    $new['tswk'] = $v['content'];
                    $new['mood'] = 5;
                    $new['file_name'] = $value['file_name'];
                    $new['file_url'] = $value['file_url'];
                    $new['add_uid'] = $value['add_uid'];
                    $new['add_time'] = $value['add_date'];
                    $new['add_user'] = get_userName($value['add_uid']);
                    $new['add_date'] = $new['add_time'];
                    $new['year'] = $value['year'];
                    $new['week'] = $value['week'];
                    $new['year_week'] = $value['year_week'];
                    $new['old_id'] = $k;
                    $db_projects_weekly->add($new);
                }
            }

        }
        $res_weekly = $db_projects_weekly->select();
        foreach ($res_weekly as $k=>$v){
            foreach ($res_weekly_reply as $reply){
                if ($v['old_id'] == $reply['mid']){
                    $data = array();
                    $where['old_id'] = $v['old_id'];
                    $data['reply_content'] = $reply['answer_content'];
                    $data['reply_uid'] = $reply['add_uid'];
                    $data['reply_user'] = get_userName($reply['add_uid']);
                    $data['reply_date'] = get_userName($reply['add_date']);
                    if (!empty($reply['add_uid'])){
                        $data['reply'] = '0';
                    }
                    $db_projects_weekly->where($where)->save($data);
                }
            }
        }

        echo '执行完毕';
        die;
    }

    //周报匹配的拓展ID
    public function weekly_project()
    {
        $db_projects = M("db_projects");
        $db_projects_weekly = M("db_projects_weekly");
        $res_projects = $db_projects->select();
        $res_projects_weekly = $db_projects_weekly->select();
        foreach ($res_projects_weekly as $k=>$v){
            foreach ($res_projects as $value){
                if ($v['pid'] == $value['pid']){
                    $where['pid'] = $v['pid'];
                    $new['project_id'] = $value['project_id'];
                    $db_projects_weekly->where($where)->save($new);
                }
            }
        }
        echo '执行完毕';
        die;
    }

}